package com.liwenfeng.diancanspringboot.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.liwenfeng.diancanspringboot.common.constant.StatusConstant;
import com.liwenfeng.diancanspringboot.common.exception.DeletionNotAllowedException;
import com.liwenfeng.diancanspringboot.common.result.PageResult;
import com.liwenfeng.diancanspringboot.mapper.DishFlavorMapper;
import com.liwenfeng.diancanspringboot.mapper.DishMapper;
import com.liwenfeng.diancanspringboot.mapper.SetmealDishMapper;
import com.liwenfeng.diancanspringboot.mapper.SetmealMapper;
import com.liwenfeng.diancanspringboot.pojo.dto.DishDTO;
import com.liwenfeng.diancanspringboot.pojo.dto.DishPageQueryDTO;
import com.liwenfeng.diancanspringboot.pojo.entity.Dish;
import com.liwenfeng.diancanspringboot.pojo.entity.DishFlavor;
import com.liwenfeng.diancanspringboot.pojo.entity.Setmeal;
import com.liwenfeng.diancanspringboot.pojo.entity.SetmealDish;
import com.liwenfeng.diancanspringboot.pojo.vo.DishVO;
import com.liwenfeng.diancanspringboot.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    //更新菜品信息
    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);

        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDTO.getId()));
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    //删除菜品
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        // 判断菜品是否在售
        if (dishMapper.countByStatus(ids, StatusConstant.ENABLE) > 0) {
            // 如果有在售菜品，抛出业务异常
            throw new DeletionNotAllowedException("存在在售菜品，不能删除");
        }
        List<Long> setmealIds = setmealDishMapper.getSetmealByDishIds(ids);
        if (setmealIds != null && setmealIds.size() > 0) {
            // 如果有关联的套餐，抛出业务异常
            throw new DeletionNotAllowedException("有关联的套餐，不能删除");
        }
        dishMapper.deleteByIds(ids);
        dishFlavorMapper.deleteByDishIds(ids);
    }

    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    //根据id查询菜品
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        Dish dish = dishMapper.getById(id);
        if (dish != null) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish, dishVO);
            dishVO.setFlavors(dishFlavorMapper.getByDishId(dish.getId()));
            return dishVO;
        }
        return null;
    }

    //根据分类id查询菜品
    @Override
    public List<DishVO> listByCategoryId(long categoryId) {
        List<Dish> dishes = dishMapper.listByCategoryId(categoryId);
        return dishes.stream().map(dish -> {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish, dishVO);
            dishVO.setFlavors(dishFlavorMapper.getByDishId(dish.getId()));
            return dishVO;
        }).toList();
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        if (Objects.equals(status, StatusConstant.DISABLE)) {
            List<SetmealDish> setmeals = setmealDishMapper.getSetmealByDishId(id);
            if (setmeals != null && setmeals.size() > 0) {
                for (SetmealDish setmealDish : setmeals) {
                    Setmeal setmeal = new Setmeal();
                    setmeal.setId(setmealDish.getSetmealId());
                    setmeal.setStatus(StatusConstant.DISABLE);
                    setmealMapper.update(setmeal);
                }
            }
        }
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }

    // 查询菜品列表，用于图片清理
    @Override
    public List<Dish> list() {
        List<Dish> dishes = dishMapper.list(new Dish());
        return dishes;
    }

}
